package cn.superchart.module.role.service.impl;

import cn.superchart.common.base.BaseServiceImpl;
import cn.superchart.common.enums.ExceptionEnum;
import cn.superchart.common.exception.ScException;
import cn.superchart.common.utils.Checker;
import cn.superchart.module.role.entity.ScRole;
import cn.superchart.module.role.mapper.ScRoleMapper;
import cn.superchart.module.role.param.*;
import cn.superchart.module.role.service.ScRoleService;
import cn.superchart.module.rolemenu.param.RoleMenuAddParam;
import cn.superchart.module.rolemenu.service.RoleMenuService;
import cn.superchart.module.uerole.service.ScUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ScRoleServiceImpl extends BaseServiceImpl<ScRole, ScRoleMapper> implements ScRoleService {

    @Autowired
    private ScUserRoleService scUserRoleService;

    @Autowired
    private RoleMenuService roleMenuService;


    @Override
    public void add(ScRoleAddParam scUserAddParam) {
        String pk = super.saveData(scUserAddParam);
        log.info(pk);
    }

    @Transactional
    @Override
    public void edit(ScRoleEditParam scRoleEditParam) {
         checkIsSysRole(scRoleEditParam.getId());
         super.updateData(scRoleEditParam);
    }

    @Override
    public ScRoleDetailParam detail(String id) {
        return queryData(id,ScRoleDetailParam.class);
    }

    @Override
    public void initInstall(String userId) {
        ScRole scRole =queryRole();
        ScRoleInitParam scRoleInitParam = new ScRoleInitParam();
        scRoleInitParam.setDescription("初始化系统角色!");
        String roleId;
        if(Checker.BeNull(scRole)){
            roleId = super.saveData(scRoleInitParam);
        }else{
            roleId = scRole.getId();
        }
        scUserRoleService.initUserRole(userId,roleId);
    }

    @Override
    public Page<ScRoleDetailParam> page(ScRolePageParam scRolePageParam) {
        return super.page(scRolePageParam,ScRoleDetailParam.class);
    }


    @Transactional
    @Override
    public boolean delete(String pk) {
        checkIsSysRole(pk);
        return super.delete(pk);
    }

    @Override
    public List<String> menuIds(String roleId) {
        List<String> menuIds = baseMapper.menuIdsByRoleId(roleId);
        return listToEmpty(menuIds);
    }

    @Transactional
    @Override
    public void assignMenus(ScAssignMenuParam scAssignMenuParam) {
        String roleId = scAssignMenuParam.getRoleId();
        roleMenuService.removeByRoleId(roleId);
        List<RoleMenuAddParam> roleMenuAddParams = new ArrayList<>();
        for(String menuId:scAssignMenuParam.getMenuIds() ){
            RoleMenuAddParam roleMenuAddParam = new RoleMenuAddParam();
            roleMenuAddParam.setRoleId(roleId);
            roleMenuAddParam.setMenuId(menuId);
            roleMenuAddParams.add(roleMenuAddParam);
        }
        roleMenuService.saveData(roleMenuAddParams);
    }

    private void checkIsSysRole(String pk){
        ScRole scRole = super.queryEntity(pk);
        if("Y".equals(scRole.getIsSystem())){
            throw new ScException(ExceptionEnum.CAN_NOT_DEL_SYS_ROLE);
        }
    }

    private ScRole queryRole() {
        QueryWrapper<ScRole> queryWrapper =queryWrapper();
        queryWrapper.lambda().eq(ScRole::getIsSystem, "Y");
        return super.getOne(queryWrapper);
    }

}
